package com.lzy.webapi.facade;

import com.lzy.base.apis.SysUserAdminServiceApi;
import com.lzy.base.dtos.SysUserAdminDto;
import com.lzy.framework.core.datasource.multiple.DataSource;
import com.lzy.product.apis.*;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.*;
import com.lzy.webapi.response.ProgressClassifyResponse;
import com.lzy.webapi.response.WorkProgressResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WIN7 on 2017/12/13.
 */
@Component
public class WorkProgressFacade {

    @Autowired
    SysUserAdminServiceApi userAdminServiceApi;

    @Autowired
    ProductClassifyDeptServiceApi classifyDeptServiceApi;

    @Autowired
    ProductInfoServiceApi infoServiceApi;

    @Autowired
    OrderProductServiceApi orderProductServiceApi;

    @Autowired
    ProductSaleLevelServiceApi saleLevelServiceApi;

    @Autowired
    ConditionServiceApi conditionServiceApi;

    @Autowired
    ProductSaleConditionServiceApi saleConditionServiceApi;

    /**
     * 获取分类list
     *
     * @param userId
     * @return
     */
    @DataSource(name = DataSource.DATASOURCE_PRODUCT)
    public List<ProgressClassifyResponse> getProgressClassifyList(Long userId) {
        //根据用户id查询出部门id
        List<ProgressClassifyResponse> progressClassifyResponseList = new ArrayList<>();
        SysUserAdminDto userAdminDto = userAdminServiceApi.selectByPrimaryKey(userId);
        if (userAdminDto != null && userAdminDto.getDeptId() != null) {
            //根据部门id查询出分类id的集合
            ProductClassifyDeptDto classifyDeptDto = new ProductClassifyDeptDto();
            classifyDeptDto.setDeptId(userAdminDto.getDeptId());
            List<ProductClassifyDeptDto> classifyDeptDtoList = classifyDeptServiceApi.selectProductClassifyDept(classifyDeptDto);
            if (classifyDeptDtoList != null && classifyDeptDtoList.size() > 0) {
                for (ProductClassifyDeptDto dto : classifyDeptDtoList) {
                    if (dto != null && dto.getClassifyId() != null) {
                        ProgressClassifyResponse progressClassifyResponse = new ProgressClassifyResponse();
                        progressClassifyResponse.setCategoryId(String.valueOf(dto.getClassifyId()));
                        //根据审核通过、上架、启用查询出产品信息
                        ProductInfoDto productInfoDto = new ProductInfoDto();
                        productInfoDto.setShelves(BusinessType.ShelfEnum.PUT.getK());
                        productInfoDto.setStatus(BusinessType.StatusEnum.ENABLED.getCode());
                        productInfoDto.setVerifyStatus(BusinessType.VerifyStatusEnum.YESVERIFY.getK());
                        List<ProductInfoDto> infoDtoList = infoServiceApi.selectListByClassifyId(productInfoDto, dto.getClassifyId());
                        String proName = "";
                        if (infoDtoList != null && infoDtoList.size() > 0) {
                            for (ProductInfoDto piDto : infoDtoList) {
                                if (piDto != null && piDto.getName() != null) {
                                    proName = proName + piDto.getName() + "、";
                                }
                            }
                            progressClassifyResponse.setProductName(proName.substring(0, proName.length() - 1));
                            progressClassifyResponseList.add(progressClassifyResponse);
                        }
                    }
                }
            }
        }
        return progressClassifyResponseList;
    }

    /**
     * 获取业务增员信息
     *
     * @param userId
     * @param classifyId
     * @return
     */
    @DataSource(name = DataSource.DATASOURCE_PRODUCT)
    public WorkProgressResponse getProgressClassifyDetail(Long userId, Long classifyId) {
        WorkProgressResponse workProgressResponse = new WorkProgressResponse();
        //根据分类id和业务员id查询出该业务员在该分类下已完成的订单列表
        OrderProductDto orderProductDto = new OrderProductDto();
        orderProductDto.setCreateUser(userId);
        orderProductDto.setStatus(BusinessType.OrderType.PAYSUCCESS.getCode());
        orderProductDto.setType(BusinessType.OrderProductType.PRODUCT.getCode());
        List<OrderProductDto> orderProductDtoList = orderProductServiceApi.selectListByClassifyId(orderProductDto, classifyId);
        if (orderProductDtoList != null && orderProductDtoList.size() > 0) {
            //存放已完成的订单的数量
            Integer productOrders = orderProductDtoList.size();
            //存放订单号的数组
            String[] orderArr = new String[orderProductDtoList.size()];
            for (int i = 0; i < orderProductDtoList.size(); i++) {
                OrderProductDto opDto = orderProductDtoList.get(i);
                if (opDto != null && opDto.getOrderId() != null) {
                    orderArr[i] = String.valueOf(opDto.getOrderId());
                }
            }
            workProgressResponse.setOrderArr(orderArr);
            workProgressResponse.setProductOrders(productOrders);
        }
        //根据分类id查询出增员标准(每个分类只有一个增员标准)
        ProductSaleLevelDto saleLevelDto = new ProductSaleLevelDto();
        saleLevelDto.setClassifyId(classifyId);
        List<ProductSaleLevelDto> saleLevelDtoList = saleLevelServiceApi.query(saleLevelDto);
        if (saleLevelDtoList != null && saleLevelDtoList.size() == 1
                && saleLevelDtoList.get(0).getConditionId() != null && saleLevelDtoList.get(0).getId() != null) {
            ConditionDto conditionDto = conditionServiceApi.findById(saleLevelDtoList.get(0).getConditionId());
            if (conditionDto != null) {
                if (conditionDto.getUnit() != null) {
                    //添加计量单位
                    workProgressResponse.setUnit(conditionDto.getUnit());
                }
                //查询增员条件
                ProductSaleConditionDto saleConditionDto = new ProductSaleConditionDto();
                saleConditionDto.setConditionId(saleLevelDtoList.get(0).getConditionId());
                saleConditionDto.setSaleId(saleLevelDtoList.get(0).getId());
                List<ProductSaleConditionDto> saleConditionDtoList = saleConditionServiceApi.query(saleConditionDto);
                if (saleConditionDtoList != null && saleConditionDtoList.size() == 1) {
                    if (saleConditionDtoList.get(0) != null && saleConditionDtoList.get(0).getStages() != null) {
                        String stages = saleConditionDtoList.get(0).getStages();
                        String[] strArray = null;
                        strArray = stages.split(",");
                        Map<String, String> orderPeople = new HashMap<>(strArray.length);
                        for (int r = 0; r < strArray.length; r++) {
                            if (strArray[r] != null) {
                                orderPeople.put(strArray[r], "增员");
                            }
                        }
                        workProgressResponse.setOrderPeople(orderPeople);
                    }
                }
            }
        }
        return workProgressResponse;
    }

}
